<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        #box {
            width: 120px;
            height: 120px;
            background-color: pink;
            position: absolute;
            left: 120px;
            top: 200px;
        }
    </style>
</head>

<body>
    <div id="box"></div>
    <input type="button" value="点击向右移动">
</body>
<script>
    //让盒子向右移动到800px
    //获取元素
    //声明一个定时器
    var timer = null;
    var oBox = document.getElementById("box");
    var aInp = document.getElementsByTagName("input");
    //添加点击事件
    aInp[0].onclick = function () {
        //添加一个定时器
        timer = setInterval(function () {
            var dis = oBox.offsetLeft + 8;
            if (dis >= 800) {
                dis = 800;
            }
            oBox.style.left = dis + 'px';
            if(dis==800){
                clearInterval(timer);
                timer=null;
            }
        }, 20)
    }

</script>

</html>